// 收到3开头的消息
import { resArr } from '../socketOnMessageTools'
import { ifContactInList, dealMessage, decryptMessage } from '../socketTools'
import store from '@/store'
import tools from '@/utils/tools'
import router from '@/router'

// 解析消息
const decryptMsg = decryptMessage
// 判断缓存的用户列表中是否有该用户
const ifContactInContactlist = ifContactInList
// 处理消息
const dealMsg = dealMessage

const dataDealer = tools.dataDealer

export default async (type, result) => {
  switch (type) {
    // 群聊-被拉入新群聊
    case '3.1.1': {
      decryptMsg(result.body, ['group_id', 'user_img', 'group_name'], 0)
      const realData = resArr[0]
      if (realData.user_img) {
        ifContactInContactlist()
      } else {
        store.dispatch('groups/getGroupInfo', realData.group_id).then(res => {
          store.commit('chatInformation/add_group', {
            groupID: res.data.data.group_id,
            groupInfo: {
              group_name: res.data.data.group_name,
              group_sponsor: res.data.data.group_sponsor,
              newCreate: true
            }
          })
        })
      }
      break
    }
    // 群聊-群聊被解散
    case '3.1.2': {
      decryptMsg(result.body, ['group_id'], 0)
      const realData = resArr[0]
      // 判断最近联系人列表中是否有该群组
      if (store.state.chatInformation.chatInformation.some(item => item.id === realData.group_id && item.id_type === 2)) {
        // 判断自己是否是该群的群主
        if (tools.isSelf(store.state.chatInformation.groupList[realData.group_id].group_sponsor)) {
          store.commit('chatInformation/remove_contact', {
            id: realData.group_id,
            idType: 2
          })
          store.commit('chatInformation/set_current_contact', {})
        }
        store.commit('chatInformation/set_group_dissolution', realData.group_id)
      }
      break
    }
    // 群聊-某人退出群聊
    case '3.1.3': {
      decryptMsg(result.body, ['group_id', 'id'], 0)
      const realData = resArr[0]
      // 判断退出群聊的是否为自己，如果是自己，则删除最近联系人该群组
      if (tools.isSelf(realData.id)) {
        store.commit('chatInformation/remove_contact', {
          id: realData.group_id,
          idType: 2
        })
        store.commit('chatInformation/set_current_contact', {})
      } else {
        // 不是自己，判断是否在当前群聊
        if (store.state.chatInformation.current_contact.id === realData.group_id && store.state.chatInformation.current_contact.id_type === 2) {
          store.dispatch('groups/getGroupMemberList', realData.group_id)
        }
        // 特殊消息通知
        await ifContactInContactlist(realData.id, 1)
        store.commit('chatInformation/add_Message', {
          message: {
            special: true,
            type: 'exitGroup',
            timestamp: +new Date(),
            exitUser: realData.id
          },
          id: realData.group_id,
          idType: 2
        })
      }
      break
    }
    // 群聊-群主退出群聊
    case '3.1.4': {
      decryptMsg(result.body, ['group_id', 'id'], 0)
      const realData = resArr[0]
      // 判断退出群聊的是否为自己，如果是自己，则删除最近联系人该群组
      if (tools.isSelf(store.state.chatInformation.groupList[realData.group_id].group_sponsor)) {
        store.commit('chatInformation/remove_contact', {
          id: realData.group_id,
          idType: 2
        })
        store.commit('chatInformation/set_current_contact', {})
      } else {
        // 不是自己，判断是否在当前群聊
        if (store.state.chatInformation.current_contact.id === realData.group_id && store.state.chatInformation.current_contact.id_type === 2) {
          store.dispatch('groups/getGroupMemberList', realData.group_id)
        }
        // 特殊消息通知
        await ifContactInContactlist(realData.id, 1)
        store.commit('chatInformation/add_Message', {
          message: {
            special: true,
            type: 'exitGroup',
            timestamp: +new Date(),
            exitUser: store.state.chatInformation.groupList[realData.group_id].group_sponsor
          },
          id: realData.group_id,
          idType: 2
        })
      }
      // 修改群组缓存列表中的群主
      store.commit('chatInformation/update_sponsor', {
        groupID: realData.group_id,
        userID: realData.id
      })
      break
    }
    // 群聊-某人被邀请入群（发给被邀请人员）
    case '3.1.5': {
      decryptMsg(result.body, ['from', 'group_id', 'group_img', 'group_name'], 0)
      const realData = resArr[0]
      // 判断是否缓存该群聊
      await ifContactInContactlist(realData.group_id, 2)
      // 将该群置顶
      const contact = {
        id_type: 2,
        messages: [],
        time: +new Date(),
        id: realData.group_id,
        name: realData.group_name,
        draft: ''
      }
      store.commit('chatInformation/set_topContact', contact)
      break
    }
    // 群聊-某人被邀请入群 (发给全员)
    case '3.1.6' : {
      decryptMsg(result.body, ['from', 'group_id', 'id'], 0)
      const realData = resArr[0]
      await ifContactInContactlist(realData.id, 1)
      await ifContactInContactlist(realData.from, 1)
      if (store.state.chatInformation.current_contact.id === realData.group_id && store.state.chatInformation.current_contact.id_type === 2) {
        store.dispatch('groups/getGroupMemberList', realData.group_id)
      }
      store.commit('chatInformation/add_Message', {
        message: {
          special: true,
          type: 'invitation',
          timestamp: +new Date(),
          inviter: realData.from,
          invitees: realData.id
        },
        id: realData.group_id,
        idType: 2
      })
      break
    }
    // 群聊-某人被踢出群聊（发给被踢的用户）
    case '3.1.7': {
      decryptMsg(result.body, ['group_id'], 0)
      const realData = resArr[0]
      store.commit('chatInformation/set_group_ifKickedOut', realData.group_id)
      break
    }
    // 群聊-修改群聊名称
    case '3.1.9': {
      decryptMsg(result.body, ['group_id', 'group_name'], 0)
      const realData = resArr[0]
      store.commit('chatInformation/update_groupName', { id: realData.group_id, name: realData.group_name })
      break
    }
    // 群聊-群聊头像发生变化
    case '3.1.10': {
      decryptMsg(result.body, ['group_id', 'user_img'], 0)
      const realData = resArr[0]
      // 设置新的群头像
      if (store.state.chatInformation.groupList[realData.group_id]) {
        store.commit('chatInformation/supplement_group_img', {
          groupID: realData.group_id,
          groupImg: dataDealer.blobToUrl(await store.dispatch('file/downloadFile', {
            fileType: 6,
            fileID: realData.user_img
          })),
          groupImgID: realData.user_img
        })
        // 如果是新创建的群聊 则在联系人列表中置顶该群聊 并给出特殊消息提示
        if (store.state.chatInformation.groupList[realData.group_id].newCreate) {
          store.commit('chatInformation/set_topContact', {
            id_type: 2,
            timestamp: +new Date(),
            messages: [],
            id: realData.group_id,
            name: store.state.chatInformation.groupList[realData.group_id].group_name,
            draft: ''
          })
          store.commit('chatInformation/clear_newCreate', realData.group_id)
          store.commit('chatInformation/add_Message', {
            message: {
              special: true,
              type: 'createGroup',
              timestamp: +new Date(),
              sponsor: store.state.chatInformation.groupList[realData.group_id].group_sponsor
            },
            id: realData.group_id,
            idType: 2
          })
          // 如果当前页面在群组页 则刷新列表
          if (router.currentRoute.fullPath === '/addressList/group') {
            store.dispatch('addressList/getGroupList')
          }
        }
      }
      break
    }
    // 群聊-某人被踢出群聊（其他人收到）
    case '3.1.11': {
      decryptMsg(result.body, ['group_id', 'id'], 0)
      const realData = resArr[0]
      // 判断是否在当前群聊
      if (store.state.chatInformation.current_contact.id === realData.group_id && store.state.chatInformation.current_contact.id_type === 2) {
        store.dispatch('groups/getGroupMemberList', realData.group_id)
      }
      await ifContactInContactlist(realData.id, 1)
      store.commit('chatInformation/add_Message', {
        message: {
          special: true,
          type: 'kicking',
          timestamp: +new Date(),
          sponsor: store.state.chatInformation.groupList[realData.group_id].group_sponsor,
          kickingUser: realData.id
        },
        id: realData.group_id,
        idType: 2
      })
      break
    }
    // 群聊-收到文本消息
    case '3.2.2': {
      decryptMsg(result.body, ['group_id', 'id', 'timestamp', 'msg_length', 'msg', 'msg_id'], 0)
      const realData = resArr[0]
      const msgObj = {
        ...realData,
        isSelf: false,
        type: 'words'
      }
      // 如果用户缓存列表中没有该群组
      await ifContactInContactlist(msgObj.group_id, 2)
      // 如果用户缓存聊表中没有该用户
      await ifContactInContactlist(msgObj.id, 1)
      // 处理消息
      dealMsg(msgObj, 2, true)
      break
    }
    // 群聊-收到语音消息
    case '3.2.4': {
      decryptMsg(result.body, ['group_id', 'id', 'timestamp', 'file_id', 'voice_length', 'msg_id'], 0)
      const realData = resArr[0]
      const msgObj = {
        ...realData,
        voiceSrc: await store.dispatch('file/downloadFile', {
          fileType: 5,
          fileID: realData.file_id,
          other: realData.group_id
        }),
        listened: false,
        isSelf: false,
        type: 'voice'
      }
      // 如果用户缓存列表中没有该群组
      await ifContactInContactlist(msgObj.group_id, 2)
      // 如果用户缓存聊表中没有该用户
      await ifContactInContactlist(msgObj.id, 1)
      // 处理消息
      dealMsg(msgObj, 2, true)
      break
    }
    // 群聊-收到图片消息
    case '3.2.6': {
      decryptMsg(result.body, ['group_id', 'id', 'timestamp', 'img_id', 'img_width', 'img_height', 'msg_id'], 0)
      const realData = resArr[0]
      const msgObj = {
        ...realData,
        imgSrc: dataDealer.blobToUrl(await store.dispatch('file/downloadFile', {
          fileType: 5,
          fileID: realData.img_id,
          other: realData.group_id
        })),
        isSelf: false,
        type: 'pic'
      }
      // 如果用户缓存列表中没有该群组
      await ifContactInContactlist(msgObj.group_id, 2)
      // 如果用户缓存列表中没有该用户
      await ifContactInContactlist(msgObj.id, 1)
      // 处理消息
      dealMsg(msgObj, 2, true)
      break
    }
    // 群聊-收到文件消息
    case '3.2.8': {
      decryptMsg(result.body, ['group_id', 'id', 'timestamp', 'file_id', 'file_size', 'file_name_length', 'file_name', 'msg_id'], 0)
      const realData = resArr[0]
      const msgObj = {
        ...realData,
        isSelf: false,
        type: 'file'
      }
      // 如果用户缓存列表中没有该群组
      await ifContactInContactlist(msgObj.group_id, 2)
      // 如果用户缓存列表中没有该用户
      await ifContactInContactlist(msgObj.id, 1)
      // 处理消息
      dealMsg(msgObj, 2, true)
      break
    }
  }
}
